package com.lcq.demo.service;

import com.lcq.demo.common.converter.ProductTypeDTOConverter;
import com.lcq.demo.common.converter.ProductTypePOConverter;
import com.lcq.demo.common.entity.PageResult;
import com.lcq.demo.entity.dto.ProductTypeDTO;
import com.lcq.demo.entity.po.ProductType;
import com.lcq.demo.repository.ProductTypeRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.EntityNotFoundException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 240701005
 */
@Service
public class ProductTypeService {

    @Resource
    private ProductTypeRepository productTypeRepository;

    /**
     * 插入数据
     */
    public Boolean insert(ProductTypeDTO productTypeDTO) throws Exception {
        if (productTypeRepository.existsById(productTypeDTO.getId())) {
            throw new Exception("产品分类ID重复！");
        }
        productTypeDTO.setIsDeleted(0);
        productTypeDTO.setCreateTime(new Date(System.currentTimeMillis()));
        ProductType type = ProductTypeDTOConverter.INSTANCE.convertToPo(productTypeDTO);
        productTypeRepository.save(type);
        return true;
    }

    /**
     * 删除数据
     */
    public Boolean deleteById(Long id) {
        productTypeRepository.deleteById(id);
        return true;
    }

    /**
     * 更新数据
     */
    public Boolean update(ProductTypeDTO productTypeDTO) {
        productTypeDTO.setUpdateTime(new Date(System.currentTimeMillis()));
        return productTypeRepository.save(ProductTypeDTOConverter.INSTANCE.convertToPo(productTypeDTO)).getId() != null;
    }

    /**
     * 根据id查询数据
     */
    public ProductTypeDTO getById(Long id) {
        return ProductTypePOConverter.INSTANCE.convertPoToDto(productTypeRepository.getById(id));
    }

    /**
     * 分页查询
     */
    public PageResult<ProductTypeDTO> getProductTypes(ProductTypeDTO productTypeDTO) {
        PageResult<ProductTypeDTO> pageResult = new PageResult<>();
        long count = productTypeRepository.count();
        Pageable pageable = PageRequest.of(productTypeDTO.getPageNum() - 1, productTypeDTO.getPageSize());
        Page<ProductType> res = productTypeRepository.findAll(pageable);
        List<ProductTypeDTO> productTypeDTOList = res.getContent().stream()
                .map(ProductTypePOConverter.INSTANCE::convertPoToDto)
                .collect(Collectors.toList());
        pageResult.setResult(productTypeDTOList);
        pageResult.setPageSize(productTypeDTO.getPageSize());
        pageResult.setPageNo(productTypeDTO.getPageNum());
        pageResult.setTotal((int) count);
        return pageResult;
    }

    /**
     * findAll
     */
    public List<ProductTypeDTO> getAllProductTypes() {
        return productTypeRepository.findAll().stream()
                .map(ProductTypePOConverter.INSTANCE::convertPoToDto)
                .collect(Collectors.toList());
    }
}
